Public Class OrderStatusReport
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblCustomerName As System.Web.UI.WebControls.Label
    Protected WithEvents ddlSupplier As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlPort As System.Web.UI.WebControls.DropDownList
    Protected WithEvents btnView As System.Web.UI.WebControls.Button
    Protected WithEvents lblWeek As System.Web.UI.WebControls.Label
    Protected WithEvents litReport As System.Web.UI.WebControls.Literal
    Protected WithEvents lblTotalItem As System.Web.UI.WebControls.Label
    Protected WithEvents btnSortOnColumn As System.Web.UI.WebControls.Button
    Protected WithEvents hidSortExpression As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblCustomerReportList As System.Web.UI.WebControls.Label
    Protected WithEvents hrefCustomerReportList As System.Web.UI.HtmlControls.HtmlAnchor

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header
    Protected WithEvents rflc As ReportFunctionsLinkCtrl

    Public Const ORDER_STATUS_REPORT_SESSION_NAME As String = "MIMS_WRS.Web.OrderStatusReport"

#Region "   Properties"
    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim cid As Integer

            If Not Security.IsCustomer Then
                Try
                    cid = CInt(Request.QueryString("cid"))
                Catch ex As Exception
                    cid = -1
                End Try
            Else
                cid = Security.GetCurrentUser.UsIdPk
            End If

            If Not cid > 0 Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return cid
        End Get
    End Property

    Protected ReadOnly Property Year() As Integer
        Get
            Try
                If IsNothing(Request.QueryString("y")) Then
                    Throw New Exception
                End If

                Dim y As Integer
                y = CInt(Request.QueryString("y"))

                If y < 0 Then
                    Throw New Exception
                End If

                Return y
            Catch ex As Exception
                Throw New ApplicationException("Invalid or missing parameter")
            End Try
        End Get
    End Property

    Protected ReadOnly Property Week() As Integer
        Get
            Try
                If IsNothing(Request.QueryString("w")) Then
                    Throw New Exception
                End If

                Dim w As Integer
                w = CInt(Request.QueryString("w"))

                If w < 1 Or w > 52 Then
                    Throw New Exception
                End If

                Return w
            Catch ex As Exception
                Throw New ApplicationException("Invalid or missing parameter")
            End Try
        End Get
    End Property

    Protected Property PageBO() As COrderStatusReport
        Get
            Return CType(Session(Me.ORDER_STATUS_REPORT_SESSION_NAME), COrderStatusReport)
        End Get
        Set(ByVal Value As COrderStatusReport)
            Session(Me.ORDER_STATUS_REPORT_SESSION_NAME) = Value
        End Set
    End Property

    Private Property SortExpression() As String
        Get
            If (ViewState("SortExpression") Is Nothing) Then
                Return ""
            Else
                Return CStr(ViewState("SortExpression"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortExpression") = Value
        End Set
    End Property

    Private Property SortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                Return " ASC"
            Else
                Return CStr(ViewState("SortDirection"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortDirection") = Value
        End Set
    End Property
#End Region

    Private Sub BindGrid()
        PageBO.ETAPortID = CInt(Me.ddlPort.SelectedItem.Value)
        PageBO.SupplierID = CInt(Me.ddlSupplier.SelectedItem.Value)
        PageBO.Week = Me.Week
        PageBO.Year = Me.Year
        PageBO.CustomerID = Me.CustomerID

        'We save some values here for the Print function.
        PageBO.CustomerName = Me.lblCustomerName.Text
        PageBO.SupplierNameAsDisplay = Me.ddlSupplier.SelectedItem.Text
        PageBO.ArrivalPortAsDisplay = Me.ddlPort.SelectedItem.Text

        PageBO.Output = Report.EReportOutputType.Screen
        PageBO.RefreshReport()

        Me.litReport.Text = PageBO.GetReportHTML
        Me.lblTotalItem.Text = PageBO.TotalItem.ToString()

    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            Response.Cache.SetCacheability(HttpCacheability.NoCache)

            Header1.NavigationBar.Add("Back to Weekly Report List", AppRoot() & "/Customer/WeeklyReportList.aspx?cid=" & Me.CustomerID.ToString)

            If Security.IsCustomer Then
                Header1.MenuIndex = EMenuIndex.CustomerWeeklyReport
                hrefCustomerReportList.Visible = False
            Else
                lblCustomerReportList.Visible = False
                Header1.MenuIndex = EMenuIndex.InternalReport
                Header1.NavigationBar.Add("Back to Customer Reports List", AppRoot() & "/Internal/Reports.aspx")
            End If

            Me.lblCustomerName.Text = CUser.GetName(Me.CustomerID)

            Me.lblWeek.Text = String.Format("Week: {0} / Year: {1}", Me.Week, Me.Year)


            Dim objReport As New COrderStatusReport(0, 0, Me.CustomerID, Me.Year, Me.Week, Report.EReportOutputType.Screen)

            Me.PageBO = objReport

            PageBO.RefreshReport()

            Me.ddlSupplier.DataSource = PageBO.Suppliers
            Me.ddlSupplier.DataTextField = "Text"
            Me.ddlSupplier.DataValueField = "Value"
            Me.ddlSupplier.DataBind()
            Me.ddlSupplier.Items.Insert(0, New ListItem("[All]", "0"))

            Me.ddlPort.DataSource = PageBO.ETAPorts
            Me.ddlPort.DataTextField = "Text"
            Me.ddlPort.DataValueField = "Value"
            Me.ddlPort.DataBind()
            Me.ddlPort.Items.Insert(0, New ListItem("[All]", "0"))

            Me.BindGrid()
        End If
    End Sub

    Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
        'Reset data.
        PageBO.SortDirection = ""
        PageBO.SortExpression = ""

        Me.BindGrid()
    End Sub

    Private Sub rflc_ExportClick() Handles rflc.ExportClick
        PageBO.Output = Report.EReportOutputType.File

        'output to user
        Response.Clear()
        Response.ContentType = "application/msexcel"
        Response.AddHeader("content-disposition", "attachment; filename=""OSR" & Me.Week & Me.Year & "_" & GetRandomFileName() & ".xls""")
        Response.Write(PageBO.GetReportHTML)
        Response.End()
    End Sub

    Private Sub btnSortOnColumn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSortOnColumn.Click

        If hidSortExpression.Value <> Me.SortExpression Then
            PageBO.SortExpression = hidSortExpression.Value
            PageBO.SortDirection = " ASC"

            Me.SortExpression = hidSortExpression.Value
            Me.SortDirection = " DESC"
        Else
            PageBO.SortExpression = hidSortExpression.Value
            PageBO.SortDirection = Me.SortDirection

            If (Me.SortDirection = " DESC") Then
                Me.SortDirection = " ASC"
            Else
                Me.SortDirection = " DESC"
            End If
        End If

        Me.BindGrid()
    End Sub
End Class
